using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using CodePlex.DnD4eCampaignManager.CampaignManagerTests.TestUtilities;
using CodePlex.DnD4eCampaignManager.DnD4e;
using CodePlex.DnD4eCampaignManager.DnD4e.Interfaces;
using CodePlex.DnD4eCampaignManager.Repositories;
using CodePlex.DnD4eCampaignManager.Repositories.XmlRepositories;
using NUnit.Framework;

namespace CodePlex.DnD4eCampaignManager.CampaignManagerTests.RepositoryTests
{
    [TestFixture]
    public class StoryArcRepositoryTests
    {
        private const string CAMPAIGN_NAME = "Test Campaign";
        private IStoryArcRepository _storyArcRepository;

        [SetUp]
        public void SetUp()
        {
            if ( Directory.Exists(CAMPAIGN_NAME) )
            {
                Directory.Delete(CAMPAIGN_NAME, true);
            }
            _storyArcRepository = new StoryArcXmlRepository(CAMPAIGN_NAME);
        }

        [Test]
        public void StoryArcRepositoryCanListAllStoryArcs()
        {
            List<string> namesFromFileSystem = new List<string>();
            foreach ( string storyArc in Directory.GetFiles(CAMPAIGN_NAME) )
            {
                string storyArcName =
                    storyArc.Replace(string.Format("{0}\\", CAMPAIGN_NAME), string.Empty).Replace(".StoryArc", string.Empty).Replace(".StoryArc",
                                                                                                                                     string.Empty);
                if ( storyArcName != string.Empty )
                {
                    namesFromFileSystem.Add(storyArcName);
                }
            }
            ReadOnlyCollection<string> storyArcsFromFileSystem = _storyArcRepository.GetAllNames();
            for ( int i = 0; i < namesFromFileSystem.Count; i++ )
            {
                Console.Out.WriteLine("storyArcsFromFileSystem [{1}] = {0}", storyArcsFromFileSystem[i], i);
                Assert.AreEqual(namesFromFileSystem[i], storyArcsFromFileSystem[i]);
            }
        }

        [Test]
        public void StoryArcRepositoryCanLoadTheRootCampaignArc()
        {
            StoryArc storyArc = DefaultTestObjects.TestStoryArc();
            storyArc.Name = CAMPAIGN_NAME;
            _storyArcRepository.Save(storyArc);
            IStoryArc rootCampaignArc = _storyArcRepository.CampaignRootArc();
            Assert.AreEqual(storyArc.Name, rootCampaignArc.Name);
            Assert.AreEqual(storyArc.Trigger, rootCampaignArc.Trigger);
            Assert.AreEqual(storyArc.Description, rootCampaignArc.Description);
            Assert.AreEqual(storyArc.Locations, rootCampaignArc.Locations);
            Assert.AreEqual(storyArc.NonPlayerCharacters, rootCampaignArc.NonPlayerCharacters);
            Assert.AreEqual(storyArc.Rewards, rootCampaignArc.Rewards);
            Assert.AreEqual(storyArc.ComponentArcNames, rootCampaignArc.ComponentArcNames);
        }

        [Test]
        public void StoryArcRepositoryCanSaveStoryArcAndGetByName()
        {
            _storyArcRepository = new StoryArcXmlRepository(CAMPAIGN_NAME);
            StoryArc storyArc = DefaultTestObjects.TestStoryArc();
            storyArc.Name = "1";
            _storyArcRepository.Save(storyArc);
            IStoryArc storyArcFromFile = _storyArcRepository.GetByName(storyArc.Name);
            Assert.AreEqual(storyArc.Name, storyArcFromFile.Name);
            Assert.AreEqual(storyArc.Trigger, storyArcFromFile.Trigger);
            Assert.AreEqual(storyArc.Description, storyArcFromFile.Description);
            Assert.AreEqual(storyArc.Locations, storyArcFromFile.Locations);
            Assert.AreEqual(storyArc.NonPlayerCharacters, storyArcFromFile.NonPlayerCharacters);
            Assert.AreEqual(storyArc.Rewards, storyArcFromFile.Rewards);
            Assert.AreEqual(storyArc.ComponentArcNames, storyArcFromFile.ComponentArcNames);
        }
    }
}